﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BLL.Managers;
using BLL;
using MessageBoxUtils;
using ToolBar = System.Windows.Controls.ToolBar;

namespace CorporateGymManagementSystem
{
    /// <summary>
    /// Interaction logic for MainApplicationWindow.xaml
    /// </summary>
    public partial class MainApplicationWindow : Window
    {
        public bool Logout { get; private set; }
        private int numberOfVisitors = 0;
        private GymWorker loggedInWorker;

        public MainApplicationWindow(GymWorker i_LoggedInWorker)
        {
            InitializeComponent();
            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
            numberOfVisitorsLabel.Content = numberOfVisitors;
            loggedInWorker = i_LoggedInWorker;
            loggedInWorkerLabel.Content = loggedInWorker.FullName;
            if (loggedInWorker.IsAdminitrator)
            {
                instructorManagementButton.IsEnabled = true;
            }
            //AbsenseNotifier n = new AbsenseNotifier();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            GymLoginManager.Instance.LogOut();
        }

        private void searchTraineeButton_Click(object sender, RoutedEventArgs e)
        {
            SearchTraineeWindow searchTraineeWindow = new SearchTraineeWindow();
            searchTraineeWindow.RegisterArrival += updateNumberOfVisitors;
            searchTraineeWindow.ShowDialog();
        }

        private void malfunctionsButton_Click(object sender, RoutedEventArgs e)
        {
            MalfunctionsListWindow malfunctionsListWindow = new MalfunctionsListWindow();
            malfunctionsListWindow.ShowDialog();
        }

        private void reportsButton_Click(object sender, RoutedEventArgs e)
        {
            ReportsWindow reportsWindow = new ReportsWindow();
            reportsWindow.ShowDialog();
        }

        private void instructorManagementButton_Click(object sender, RoutedEventArgs e)
        {
            InstructorManagementWindow instructorManagementWindow = new InstructorManagementWindow();
            bool? res = instructorManagementWindow.ShowDialog();
        }

        private void inboxButton_Click(object sender, RoutedEventArgs e)
        {
            InboxWindow inboxWindow = new InboxWindow(loggedInWorker);
            bool? res = inboxWindow.ShowDialog();
        }

        private void messageBoardButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBoardWindow messageBoardWindow = new MessageBoardWindow(loggedInWorker);
            bool? res = messageBoardWindow.ShowDialog();
        }

        private void updateNumberOfVisitors()
        {
            numberOfVisitors++;
            numberOfVisitorsLabel.Content = numberOfVisitors;
        }

        private void ButtonBar_Loaded(object sender, RoutedEventArgs e)
        {
            ToolBar toolBar = sender as ToolBar;
            var overflowGrid = toolBar.Template.FindName("OverflowGrid", toolBar) as FrameworkElement;
            if (overflowGrid != null)
            {
                overflowGrid.Visibility = Visibility.Collapsed;                
            }
        }

        private void toolBarExitButton_Click(object sender, RoutedEventArgs e)
        {
            Logout = false;
            this.Close();
        }

        private void toolBarLogoutButton_Click(object sender, RoutedEventArgs e)
        {
            Logout = true;
            this.Close();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!Logout)
            {
                e.Cancel = TryClose();
            }
            else
            {
                e.Cancel = TryLogout();
                if(!e.Cancel)
                {
                    LoginWindow loginWindow = new LoginWindow();
                    loginWindow.Show();
                }
            }
        }

        private bool TryLogout()
        {
            MessageBoxResult messageBoxResult = WPFMessageBox.Show("Are you sure you want to logout?", "Logout", MessageBoxButton.YesNo, MessageBoxImage.Question);
            bool isLogout = true;

            switch (messageBoxResult)
            {
                case MessageBoxResult.Yes:
                    isLogout = false;
                    break;
                case MessageBoxResult.No:
                    isLogout = true;
                    break;
            }

            return isLogout;
        }

        private bool TryClose()
        {
            MessageBoxResult messageBoxResult = WPFMessageBox.Show("Are you sure you want to exit?", "Exit", MessageBoxButton.YesNo, MessageBoxImage.Question);
            bool isClose = true;

            switch (messageBoxResult)
            {
                case MessageBoxResult.Yes:
                    isClose = false;
                    break;
                case MessageBoxResult.No:
                    isClose = true;
                    break;
            }

            return isClose;
        }
    }
}
